<?php
namespace app\system\controller;

use app\common\model\ShopJicaiGoodsModel;
use think\Request;

class ShopJicaiGoodsController extends BaseController
{
    private $jicaiModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->jicaiModel = new ShopJicaiGoodsModel();
    }

    /**
     * 首页
     */
    public function index()
    {
        $map       = [];
        $query     = [];
        $searchKey = $this->request->param('searchKey');
        $searchName = $this->request->param('searchName');
        if(!empty($searchName)){
            $map[$searchKey] = ['like','%'.$searchName.'%'];
            $query ['searchKey'] = $searchKey;
            $query ['searchName'] = $searchName;
        }
        $jicaiStatus = $this->request->param('jicai_status',0);
        $map['jicai_status'] = $jicaiStatus;
        $query['jicai_status'] = $jicaiStatus;
        $list = $this->jicaiModel->where($map)->order('jicai_commend asc')->paginate(10, false, [
            'query' => $query,
        ]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }
    /**
     * 是否推荐
     */
    public function commend(){
        $jicaiId = $this->request->param('jicai_id');
        $jicaiCommend = $this->request->param('jicai_commend');
        $jicaiCommend = abs($jicaiCommend-1);
        $result = $this->jicaiModel->where('jicai_id',$jicaiId)->setField('jicai_commend',$jicaiCommend);
        if($result){
            $msg = '修改成功';
        }else{
            $msg = $this->jicaiModel->getError();
        }
        $this->success($msg);
    }

    /**
     * 审核通过
     */
    public function pass(){
        $jicaiId = $this->request->post('jicaiid');
        $info = $this->jicaiModel->where('jicai_id',$jicaiId)->find();
        if($info['begin_time'] > time()){
            $jicaiStatus = 2;
        }
        if($info['end_time'] < time()){
            $jicaiStatus = 3;
        }
        if(($info['end_time'] > time())&&($info['begin_time']<time())){
            $jicaiStatus = 1;
        }
        $this->jicaiModel->where('jicai_id',$jicaiId)->setField('jicai_status',$jicaiStatus);
        $msg = '审核通过';
        $this->success($msg);
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $jicaiId = $this->request->param('jicai_id/d');
            $jicaiStatus = $this->jicaiModel->where('jicai_id',$jicaiId)->column('jicai_status');
            if($jicaiStatus == 1){
                $this->error('正在进行集采,不能删除');
            }
            $this->jicaiModel->delData($jicaiId);
            $this->success('删除成功！');
        }
    }


}
